package com.redoor.app.stake.common.utils;

import com.redoor.app.stake.common.model.CompareResultBean;

import java.lang.reflect.Field;
import java.util.*;

/**
 * Created by wangjunlei on 2018/2/23.
 */
public class CompareUtil {

    /**
     * 对比两个对象值是否相等，返回不相等的数据集合
     * @param newclass1
     * @param oldclass2
     * @param compareAttr 需要对比的大写属性名称，为空则全部对比
     * @return
     * @throws ClassNotFoundException
     * @throws IllegalAccessException
     */
    public static List<CompareResultBean> compareTwoClass(Object newclass1, Object oldclass2,List<String> compareAttr) throws ClassNotFoundException, IllegalAccessException {
        List<CompareResultBean> list = new ArrayList<CompareResultBean>();
        //获取对象的class
        Class<?> clazz1 = newclass1.getClass();
        Class<?> clazz2 = oldclass2.getClass();

        //获取对象的属性列表
        Field[] field1 = clazz1.getDeclaredFields();
        Field[] field2 = clazz2.getDeclaredFields();
        //遍历属性列表field1
        for (int i = 0; i < field1.length; i++) {
            //遍历属性列表field2
            for (int j = 0; j < field2.length; j++) {
                //如果field1[i]属性名与field2[j]属性名内容相同
                if (field1[i].getName().equals(field2[j].getName())) {

                    if(compareAttr != null && compareAttr.size() > 0 && !compareAttr.contains(field2[j].getName().toUpperCase())){
                        continue;
                    }
                    if (field1[i].getName().equals(field2[j].getName())) {
                        field1[i].setAccessible(true);
                        field2[j].setAccessible(true);
                        //如果field1[i]属性值与field2[j]属性值内容不相同
                        if (!compareTwo(field1[i].get(newclass1), field2[j].get(oldclass2))) {
                            CompareResultBean bean = new CompareResultBean();
                            bean.setName(field1[i].getName());
                            bean.setNewValue(field1[i].get(newclass1));
                            bean.setOldValue(field2[j].get(oldclass2));
//                            Map<String, Object> map2 = new HashMap<String, Object>();
//                            map2.put("name", field1[i].getName());
//                            map2.put("old", field1[i].get(class1));
//                            map2.put("new", field2[j].get(class2));
                            list.add(bean);
                        }
                        break;
                    }
                }
            }
        }
        return list;
    }

    /**
     * 对比两个数据是否内容相同
     *
     * @param object1,object2
     * @return boolean类型
     */
    public static boolean compareTwo(Object object1, Object object2) {

        if (object1 == null && object2 == null) {
            return true;
        }
        if (object1 == null && object2 != null) {
            return false;
        }
        if (object1 !=null && object2 == null) {
            return true;
        }
        if (object1.equals(object2)) {
            return true;
        }
        if(object1 instanceof Date){
            return ((Date) object1).getTime() == ((Date) object2).getTime();
        }
        return false;
    }

}
